package cn.wp.pet.service.impl;

import cn.wp.basic.domain.Point;
import cn.wp.basic.service.impl.BaseServiceImpl;
import cn.wp.basic.util.DistanceUtil;
import cn.wp.org.domain.Shop;
import cn.wp.org.mapper.ShopMapper;
import cn.wp.pet.domain.SearchMasterMsg;
import cn.wp.pet.mapper.SearchMasterMsgMapper;
import cn.wp.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private ShopMapper shopMapper;

    // 保存寻主发布的数据
    @Override
    @Transactional
    public void publish(SearchMasterMsg searchMasterMsg) {
        // 计算离用户最近的门店
        // 获取用的地址的经度
        Point point1 = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.loadAll();

        Shop shop = DistanceUtil.getNearestShop(point1, shops);
        searchMasterMsg.setShop_id(shop.getId());
        // 设置状态 待处理
        searchMasterMsg.setState(0);
        super.add(searchMasterMsg);
    }

    @Override
    public void changeState(Long id) {
        searchMasterMsgMapper.changeState(id);
    }
}
